package cn.lucky.springcloud.controller;

import cn.lucky.springcloud.entities.CommonResult;
import cn.lucky.springcloud.entities.Payment;
import cn.lucky.springcloud.lb.ConsumerRoundRobinRule;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lingpeng.jiang
 * @create 2020-12-01-22:01
 */
@RestController
@Slf4j
public class OrderController {


    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Resource
    private ConsumerRoundRobinRule consumerRoundRobinRule;

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPaymentById(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id,CommonResult.class);
    }

    @GetMapping("/consumer/payment/list")
    public CommonResult<Payment> getPaymentList(){
        return restTemplate.getForObject(PAYMENT_URL + "/payment/list",CommonResult.class);
    }

    @GetMapping("/consumer/payment/create")
    public CommonResult<Integer> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL + "/payment/create",payment,CommonResult.class);
    }


    /**
     * 使用RestTemplate的getForEntity来请求
     * @param id
     * @return
     */
    @GetMapping("/consumer/payment/getEntity/{id}")
    public CommonResult<Payment> getPaymentByIdByGetForEntity(@PathVariable("id") Long id){
        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        instances.forEach(serviceInstance -> {
            System.out.println(serviceInstance.getInstanceId());
        });
        ResponseEntity<CommonResult> forEntity = restTemplate.getForEntity(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
        if (forEntity.getStatusCode().is2xxSuccessful()){
            log.info(forEntity.getHeaders() + "\t" + forEntity.getStatusCode());
            return forEntity.getBody();
        } else {
            return new CommonResult<>(500,"内部错误");
        }
    }

    /**
     * 使用RestTemplate的postForEntity来请求
     * @param payment 支付实体
     * @return
     */
    @GetMapping("/consumer/payment/createEntity")
    public CommonResult<Integer> createByPostForEntity(Payment payment){
        ResponseEntity<CommonResult> commonResultResponseEntity = restTemplate.postForEntity(PAYMENT_URL + "/payment/create", payment, CommonResult.class);
        if (commonResultResponseEntity.getStatusCode().is2xxSuccessful()){
            return commonResultResponseEntity.getBody();
        } else {
            return new CommonResult<>(500,"新增失败");
        }
    }


    /**
     * 测试自定义负载策略算法
     * @return
     */
    @GetMapping("/consumer/payment/lb")
    public String consumerBalancerRule(){
//        List<ServiceInstance> instances = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
//        ServiceInstance serviceInstance = consumerRoundRobinRule.getServiceInstance(instances);
        return restTemplate.getForObject(PAYMENT_URL + "/payment/lb",String.class);
    }
}
